//用于显示系统列表的可编辑grid
var testSystemEditorGrid = Ext.create('Ext.grid.Panel', {
    title: '系统列表',
    store: testSystemGridStore,
    columns: [new Ext.grid.RowNumberer(), {
        text: '系统名称',
        dataIndex: 'name',
        sortable: false,
        flex: 1,
        filter: { //用于形成搜索框
            type: 'string',
            emptyText: '输入要搜索的系统名称',
            serializer: function (filter) {
                return filter.value;
            }
        }
    }, {
        text: '类型',
        dataIndex: 'type',
        sortable: false,
        flex: 1,
        renderer: function (value) {
            return value == 0 ? '实训项目' : '真实项目';
        },
        filter: { //用于形成搜索框
            type: 'string',
            emptyText: '输入要搜索的类型',
            serializer: function (filter) {
                return filter.value;
            }
        }
    }, {
        text: '创建人',
        dataIndex: 'addManName',
        sortable: false,
        flex: 1,
        filter: { //用于形成搜索框
            type: 'string',
            emptyText: '输入要搜索的类型',
            serializer: function (filter) {
                return filter.value;
            }
        }
    }, {
        text: "创建时间",
        dataIndex: 'createDate',
        flex: 0.5,
        renderer: function (value) {
            if (value == null)
                return '无';
            else
                return value.replace('T', '  ');
        }
    }, {
        text: '查看详情',
        align: 'center',
        sortable: false,
        xtype: 'actioncolumn',
        width: 90,
        items: [{
            icon: 'static/images/icons/fam/look.ico',
            tooltip: '查看详情',
            handler: function (grid, rowIndex, colIndex, actionItem, event, record, row) {
                testSystemFileArray = [];//清空formData储存内容
                testSystrmIsCreate = 0;//标注现在是编辑操作
                testSystemEditorTabPanel.systemId = record.data.id;//将系统Id储存在tabPanel中
                testSystemSeeDetailClick(record, 1);
            }
        }]
    }],
    plugins: {
        gridfilters: true //启用filters
    },
    selModel: Ext.create('Ext.selection.CheckboxModel', {
        mode: 'SINGLE',
        listeners: {
            selectionchange: function (sm, selections) {
                testSystemEditorGrid.down('#deleteSystemButton').setDisabled(selections.length === 0);
            }
        }
    }),
    dockedItems: [{
        xtype: 'toolbar',
        items: [{
            text: '添加',
            tooltip: '添加测试系统',
            itemId: 'addSystemButton',
            icon: 'static/images/icons/fam/add.gif',
            listeners: {
                click: function () {
                    Ext.getCmp('centerRegion').removeAll(false);
                    testSystrmIsCreate = 1;//标志现在是新建操作
                    testSystemCleanForm(1);
                    Ext.getCmp('centerRegion').add(testSystemEditorTabPanel);
                    testSystemDetailEditorForm.down('#devMan').setValue(currentUser);
                    testSystemDetailEditorForm.down('#addMan').setValue(currentUser);
                }
            }
        }, '-', {
            text: '删除',
            tooltip: '删除测试系统',
            itemId: 'deleteSystemButton',
            icon: 'static/images/icons/fam/delete.gif',
            listeners: {
                click: function () {
                    var item = testSystemEditorGrid.getSelectionModel().getSelected();// 得到被选择的行的数组.
                    if (item.length == 0)
                        Ext.Msg.alert('错误', '请选择要删除的测试系统！');
                    else {
                        Ext.MessageBox.confirm('删除', '你确定要将该测试系统删除吗', function (btn) {
                            if (btn === 'yes') {
                                var record = testSystemGridStore.getById(item.items[0].data.id);
                                Ext.Ajax.request({
                                    url: zuulUrl + '/' + testServiceName + '/testSystem/' + record.data.id,
                                    async: true,   //ASYNC 是否异步( TRUE 异步 , FALSE 同步)
                                    method: 'delete',
                                    success: function (response, opts) {
                                        Ext.Msg.alert('Success', '删除成功');
                                        testSystemGridStore.remove(record);
                                    },
                                    failure: function (response, opts) {
                                        Ext.Msg.alert('Error', '该功能暂时不可用');
                                    }
                                });
                            }
                        });
                    }
                }
            }
        }]
    }]
});

//用于显示系统附件列表的可编辑Grid
var testSystemAttachedEditorGrid = Ext.create('Ext.grid.Panel', {
    store: testSystemAttachedStore,
    columns: [new Ext.grid.RowNumberer(), {
        text: '附件名称',
        dataIndex: 'name',
        sortable: false,
        flex: 1,
        filter: { //用于形成搜索框
            type: 'string',
            emptyText: '输入要搜索的系统名称',
            serializer: function (filter) {
                return filter.value;
            }
        }
    }, {
        text: '大小[KB]',
        dataIndex: 'size',
        sortable: false,
    }, {
        text: '下载',
        align: 'center',
        sortable: false,
        xtype: 'actioncolumn',
        width: 90,
        items: [{
            icon: 'static/images/icons/fam/download.ico',
            tooltip: '下载附件',
            handler: function (grid, rowIndex, colIndex, actionItem, event, record, row) {
                if (record.data.isCreate)
                    Ext.Msg.alert('错误', '新附件未保存无法下载！');
                else
                    fileDownload(zuulUrl + '/' + testServiceName + '/testSystem/attached/' + record.data.id);
            }
        }]
    }],
    plugins: {
        gridfilters: true //启用filters
    },
    selModel: Ext.create('Ext.selection.CheckboxModel', {
        listeners: {
            selectionchange: function (sm, selections) {
                testSystemAttachedEditorGrid.down('#deleteAttachedButton').setDisabled(selections.length === 0);
            }
        }
    }),
    dockedItems: [{
        xtype: 'toolbar',
        items: [{
            text: '添加',
            tooltip: '添加附件',
            itemId: 'addAttachedButton',
            icon: 'static/images/icons/fam/add.gif',
            listeners: {
                click: function () {
                    document.getElementById('systemAttaInput').click();
                }
            }
        }, '-', {
            text: '删除',
            tooltip: '删除附件',
            itemId: 'deleteAttachedButton',
            icon: 'static/images/icons/fam/delete.gif',
            listeners: {
                click: function () {
                    var item = testSystemAttachedEditorGrid.getSelectionModel().getSelected();// 得到被选择的行的数组.
                    if (item.length == 0)
                        Ext.Msg.alert('错误', '请选择要删除的附件！');
                    else {
                        Ext.MessageBox.confirm('删除', '你确定要将这些附件删除吗', function (btn) {
                            if (btn === 'yes') {
                                for (var i = 0, length = item.items.length; i < length; i++) {
                                    if (item.items[i].data.isCreate) {//如果是新添加的附件的话
                                        var index = 0;
                                        var data = testSystemAttachedStore.getData().items;
                                        for (var j = 0, length2 = data.length; j < length2; j++) {//算出它是第几个附件
                                            if (data[j].data.isCreate) {
                                                if (data[j].data.id == item.items[i].data.id)
                                                    break;
                                                index++;
                                            }
                                        }
                                        testSystemFileArray.splice(index, 1);//然后再把对应储存新上传文件数据的数组对象删除
                                    }
                                    testSystemAttachedStore.remove(item.items[i]);//不然的话就只要将store的记录清除就好了
                                    i--;
                                    length--;
                                }
                            }
                        });
                    }
                }
            }
        }]
    }]
});
